<?php
class Controller_Public extends Controller_Abstract
{
	function actionFBADataImport() 
	{
		$this->_view['title'] = 'FBA资料导入';
		$this->_view['warehouse'] = Warehouse::find('type=? and status=?', 'amazon', true)->asArray()->getAll();
	}
	
	function actionFBADataImportSave()
	{
		$this->_view['title'] = 'FBA资料导入（保存）';
	/*	if ($_POST['warehouse_id'] && $_FILES)
		{
			$upload_result = Upload_Files::uploadFile('data', Q::ini('custom_flag/upload_file_type/online/value'));
			$content = file_get_contents($upload_result['path']);
			$array = explode("\n", $content);
			$data = "";
			foreach ($array as $a)
			{
				$arr = explode("\t", $a);
				if (preg_match("/B{0,1}\d{5,6}/", substr($arr[0], 0, 6)))
				{
					if(strlen($arr[1]) > 54)
					{
						$title = Helper_BSS_Normal::strMiddleReduce($arr[1], 54);
					}
					else
					{
						$title = $arr[1];
					}
					$sku = substr($arr[0], 0, 6);
					if (substr($sku, 0, 1) == "B")
					{
						$product_bind = Product_Bind::find("number=?", $sku)->asArray()->getOne();
						if ($product_bind)
						{
							$product_bind_item = Product_Bind_Item::find("product_bind_id=?", $product_bind['id'])
								->joinLeft('product_basic', 'number', 'product_basic.id=product_bind_item.product_basic_id')
								->asArray()->getAll();
							foreach ($product_bind_item as $p)
							{
								QDB::getConn()->execute('update product_warehouse set storage_title=' . "'" . mysql_escape_string($title) . "'" . ', storage_number=' .  "'" . $arr[3] .  "'" .' where product_basic_id=(select id from product_basic where number=\'' . $p['number'].  '\') and warehouse_id=' . $_POST['warehouse_id'] . ';');
							}
						}
					} 
					else
					{
						QDB::getConn()->execute('update product_warehouse set storage_title=' . "'" . mysql_escape_string($title) . "'" . ', storage_number=' .  "'" . $arr[3] .  "'" .' where product_basic_id=(select id from product_basic where number=\'' . $sku .  '\') and warehouse_id=' . $_POST['warehouse_id'] . ';');
					}
				}
			}
			return $this->_redirectMessage('导入成功', '正在返回..', url('Default::Public/FBADataImport'), SUCCESS);
		}*/
	}
	
	function actionExpressPriceQuery()
	{
		$this->_view['title'] = '快递价格查询';
		$country = Country::find('code <> \'\'')->order('code asc')->asArray()->getAll();
		$this->_view['country'] = $country;
		$shipping_method = Shipping_Method::getActiveShippingMethod();
		$this->_view['shipping_method'] = $shipping_method;
		if ((isset($_GET['shipping_method']) && $_GET['shipping_method']) && (isset($_GET['country']) && $_GET['country']) && (isset($_GET['weight']) && $_GET['weight']))
		{
			$result = array();
			foreach ($_GET['country'] as $c)
			{
				$country = Country::find('code=?', $c)->asArray()->getOne();
				foreach ($_GET['shipping_method'] as $sm)
				{
					$formula = Shipping_Method::getShippingMethodByCode($sm, 'formula');
					if ($formula)
					{
						$variable = array('weight' => $_GET['weight'], 'country_code' => $c);
						eval($formula);
						$currency = Currency::find('code=?', $formula_result['postage_currency_code'])->asArray()->getOne();
						$formula_result['postage'] = ceil($formula_result['postage'] * 100) / 100;
						$formula_result['postage_currency_symbol'] = $currency['symbol'];
						$formula_result['cn_name'] = $country['cn_name'];
						$formula_result['country_code'] = $country['code'];
						$formula_result['code'] = $sm;
						$result[] = $formula_result;
					}
					else
					{
						$r['ack'] = SUCCESS;
						$r['message'] = '';
						$r['postage'] = 0;
						$r['postage_currency_code'] = '';
						$r['postage_currency_symbol'] = '';
						$r['cn_name'] = $country['cn_name'];
						$r['country_code'] = $country['code'];
						$r['code'] = $sm;
						$result[] = $r;
					}
				}
			}
			$postage_sort = Helper_BSS_Array::SortByValue($result, 'postage', 'desc');
			$this->_view['result'] = $postage_sort;
		}
	}
	
	function actionProductNumberFBA()
	{
		$this->_view['title'] = '打印海外仓标签';
		if ($this->_context->isPOST())
		{
			$product = array('quantity' => (int)trim($_POST['quantity']), 'number' => trim($_POST['number']));
			OLM_Barcode::productNumberFBA($product, $_POST['country']);
		}
	}
	
	function actionCountryList()
	{
		$this->_view['title'] = '国家对照表';
		$country = Country::find()->asArray()->getAll();
		$this->_view['country'] = $country;
	}
	
	function actionProductInGZWarehouse()
	{
		$this->_view['title'] = '查询广州仓产品信息';
		$columns = array(
			'product_basic.number,product_basic.name',
			'product_warehouse.available_quantity,product_warehouse.actual_quantity,product_warehouse.shelf_position,product_warehouse.product_feature,product_warehouse.sales_status,product_warehouse.purchase_status'
		);
		$product = Product_Warehouse::find('product_warehouse.warehouse_id=1 and product_basic.number=?', $_GET['number'])
			->joinLeft('product_basic', '', 'product_basic.id=product_warehouse.product_basic_id')
			->setColumns($columns)->asArray()->getOne();
		if ($product)
		{
			$product['sales_status'] = Helper_BSS_Normal::getIniOne('product_sales_status', 'value', $product['sales_status'], 'name');
			$product['purchase_status'] = Helper_BSS_Normal::getIniOne('product_purchase_status', 'value', $product['purchase_status'], 'name');
			$ack = SUCCESS;
		}
		else
		{
			$ack = FAILURE;
		}
		echo json_encode(array('ack' => $ack, 'product' => $product));exit;
	}
	
	function actionUpdateShelfInGZWarehouse()
	{
		$this->_view['title'] = '更新广州仓架位';
		$condition = Helper_BSS_Normal::buildCondition();
		if (empty($condition['key']) || $condition['key'] != Q::ini('custom_system/pda_key'))
		{
			echo json_encode(array('ack' => FAILURE, 'message' => ''));
			exit;
		}
		$product_basic = Product_Basic::find('number=?', $condition['product_number'])->asArray()->getOne();
		if ( ! $product_basic)
		{
			echo json_encode(array('ack' => FAILURE, 'message' => '此产品编号不存在'));
			exit;
		}
		$product_warehouse_result = Product_Warehouse::find('product_basic_id=? and warehouse_id=1', $product_basic['id'])->getOne();
		$product_warehouse_result->shelf_position = $condition['shelf_position'];
		$product_warehouse_result->save();
		echo json_encode(array('ack' => SUCCESS, 'shelf_position' => $product_warehouse_result['shelf_position'], 'message' => '更新架位成功'));
		exit;
	}
	
	function actionGetAmazonFeedSubmissionResult()
	{
		$this->_view['title'] = '查询Amazon上传处理结果';
		$amazon1 = Shop_User_Mapping::getShopByPlatform('IAINSWAN');
		$amazon2 = Shop_User_Mapping::getShopByPlatform('DAVIDSONNE');
		$amazon3 = Shop_User_Mapping::getShopByPlatform('Orr');
		$amazon4 = Shop_User_Mapping::getShopByPlatform('robertaspland');
		$amazon5 = Shop_User_Mapping::getShopByPlatform('NNJYCE');
		$amazon6 = Shop_User_Mapping::getShopByPlatform('shariwilliams');
		$amazon7 = Shop_User_Mapping::getShopByPlatform('TODD');
		$this->_view['shop'] = array_merge($amazon1, $amazon2, $amazon3, $amazon4, $amazon5, $amazon6, $amazon7);
		if (!empty($_GET['shop_id']) && !empty($_GET['id']))
		{
			Helper_BSS_API::getAmazonFeedSumissionResult($_GET['shop_id'], $_GET['id']);
		}
	}
}